package com.qfoud.edu.shiro.framwork.filter;


import com.qfoud.edu.shiro.framwork.exception.ShiroException;
import com.qfoud.edu.shiro.framwork.slot.base.BaseSlot;
import com.qfoud.edu.shiro.framwork.slot.impl.AuthorizeSlot;
import com.qfoud.edu.shiro.framwork.slot.impl.AuthticationSlot;
import com.qfoud.edu.shiro.framwork.slot.impl.MethodAuthorizeSlot;
import com.qfoud.edu.shiro.framwork.utils.StringUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xiaobobo
 * @title: GlobleProxyFilter
 * @projectName cd-java-2303-project-parent
 * @description: 这里写一个全局的过滤器去触发我们的链条的执行
 * @date 2023/5/15  15:00
 */
public class GlobleProxyFilter implements Filter {

    private  List<BaseSlot> baseSlotList=new ArrayList<>();
    //这里还需要进行静态资源的释放

    private static final String[] noIntercept={
      ".jpg",".css",".js"
    };

    @Override
    public void init(FilterConfig filterConfig) {
        //这里还需要获取设置的登陆的地址
        String loginUrl = filterConfig.getInitParameter("loginUrl");
        //获取登陆页面的地址
        String loginPage = filterConfig.getInitParameter("loginPage");


        String unAuthPage = filterConfig.getInitParameter("unAuthPage");
        if(StringUtils.isEmpty(loginPage)){
            throw new ShiroException("登陆页面未配置");
        }
        if(StringUtils.isEmpty(loginUrl)){
           throw new ShiroException("未设置登陆地址");
        }
        if(StringUtils.isEmpty(unAuthPage)){
            throw new ShiroException("未设置没有权限访问的时候,访问的页面");
        }
        //这里需要初始化我们的链条
        baseSlotList.add(new AuthorizeSlot(loginPage,loginUrl));
        baseSlotList.add(new AuthticationSlot(loginUrl));
        baseSlotList.add(new MethodAuthorizeSlot(loginPage,loginUrl,unAuthPage));
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //获取请求地址
        HttpServletRequest req= (HttpServletRequest) servletRequest;
        String requestURI = req.getRequestURI();
        if(checkNoIntercept(requestURI)){
           filterChain.doFilter(servletRequest,servletResponse);
           return;
        }
        //在这个位置 就要依次去执行我们的Slot
        try {
            for (int i = 0; i <baseSlotList.size() ; i++) {
                BaseSlot baseSlot = baseSlotList.get(i);
                baseSlot.dealRequestAndResponse(servletRequest,servletResponse);
            }
            //整个链条执行完成如果是没有异常的话那么直接放行
            filterChain.doFilter(servletRequest,servletResponse);
        }catch (Exception err){
            //出现异常就执行到这里....
            //err.printStackTrace();
           // System.out.println("执行链条出错");
        }
    }

    /**
     * 检测需要放行的资源
     * @param requestURI
     * @return
     */
    private boolean checkNoIntercept(String requestURI) {
        for (String path:noIntercept) {
            if(requestURI.endsWith(path)){
                return true;
            }
        }
        return false;
    }
}
